#include "MainWindow.h"
#include "Regwindow.h"
#include "FoodTable.h"
#include "UserTable.h"
#include "FoodOrderTable.h"
#include "WelcomWindow.h"
#include "CurrentUser.h"
#include "SelfInformWindow.h"
#include "FoodOderWindow.h"
#include "ShopCartWindow.h"
#include "AdminMainWindow.h"
#include "AdminAddNewFoodWindow.h"
#include "AdminAddAdminWindow.h"
#include "AdminFoodOrderListWindow.h"
#include "UserFoodOrderListWindow.h"
#include "AdminFoodWindow.h"
//#include "foodorderitem.h"

#include <QApplication>
#include <QWidget>
#include <QSqlError>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QString>
#include <QDebug>

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE","KFC"); //数据库

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    //连接数据库
    QString basename = "E://qt/database/KFC.db";
    db.setHostName("127.0.0");
    db.setDatabaseName(basename);
    db.setUserName("ichen");
    db.setPassword("123");
    db.open();
//    if(!db.open()){
//        qDebug()<<"open error:"<<db.lastError();
//    }

    //创建食物表food_table
    FoodTable food_table;
    food_table.createFoodTable();
//    food_table.insertDefaultFood(); //插入默认的食物
    //创建用户表user_table
    UserTable user_table;
    user_table.createUserTable();
    //创建当前用户表
    CurrentUser current_user;
    current_user.createCurrentUserTable();
    //创建食物订单表food_order_table
    FoodOrderTable food_order_table;
    food_order_table.createFoodOrderTable();

    MainWindow login_window;    //登陆界面
    RegWindow reg_window;   //注册界面
    WelcomWindow welcom_window; //欢迎界面
    SelfInformWindow self_inform_window;    //个人信息界面
    FoodOderWindow food_order_window;   //订餐界面
    ShopCartWindow shop_cart_window;    //购物车界面
    AdminMainWindow admin_main_window;  //管理员菜单界面
    AdminAddNewFoodWindow admin_add_new_food_window;    //添加食物界面
    AdminAddAdminWindow admin_add_admin_window; //添加管理员账户界面
    AdminFoodOrderListWindow admin_food_order_list_window;  //订单界面列表
    UserFoodOrderListWindow user_food_order_list_window;    //个人订单界面
    AdminFoodWindow admin_food_window;  //管理食物界面
//    FoodOrderItem food_order_item;

    /**
      * @leiteorz
      * 如果说当前用户未登出,那么再次登陆的时候也应该是显示当前用户
      * 所以要先做一个判断,是显示login_window()还是welcom_window()
      */
    User currentU = current_user.queryCurrentUser();
    if(currentU.name != ""){
        if(currentU.category == 0) welcom_window.show();
        else if(currentU.category == 1) admin_main_window.show();
    }else{
        login_window.show();
    }

    //关联登陆界面和注册界面的信号与槽函数
    QObject::connect(&login_window,SIGNAL(showRegWindow()),&reg_window,SLOT(replyShowReg()));   //登陆界面到注册界面
    QObject::connect(&reg_window,SIGNAL(backToLoginWin()),&login_window,SLOT(replyBackToLogin()));  //注册界面返回登陆界面
    //关联登陆界面和管理员菜单界面
    QObject::connect(&login_window,SIGNAL(showAdminMainWindow()),&admin_main_window,SLOT(replyShowAdminMainWindow()));  //从登陆界面到管理员菜单界面
    QObject::connect(&admin_main_window,SIGNAL(showMainWindow()),&login_window,SLOT(replyBackToLogin()));   //从管理员界面回到登陆界面
    //关联登陆界面和欢迎界面的信号与槽函数
    QObject::connect(&login_window,SIGNAL(showWelcomWindow()),&welcom_window,SLOT(replyShowWelcom()));  //登陆界面到欢迎界面
    //关联欢迎界面和个人信息界面的信息与槽函数
    QObject::connect(&welcom_window,SIGNAL(showSelfInformWindow()),&self_inform_window,SLOT(replyShowSelfInformWin())); //从欢迎界面到个人信息界面
    QObject::connect(&self_inform_window,SIGNAL(backToWelcomWindow()),&welcom_window,SLOT(replyShowWelcom()));  //从个人信息界面回到欢迎界面
    //关联个人信息界面和登陆界面的信息与槽函数
    QObject::connect(&self_inform_window,SIGNAL(backToMainWindow()),&login_window,SLOT(replyBackToLogin()));  //从个人信息界面回到登陆界面
    //关联个人信息界面和个人订单界面
    QObject::connect(&self_inform_window,SIGNAL(showUserFoodOrderListWindow())
                     ,&user_food_order_list_window,SLOT(replyShowUserFoodOrderListWindow()));   //从个人信息界面显示个人订单界面
    //关联欢迎界面和订餐界面
    QObject::connect(&welcom_window,SIGNAL(showOrderFoodWindow()),&food_order_window,SLOT(replyShowFoodOrder()));   //从欢迎界面跳转到订餐界面
    QObject::connect(&food_order_window,SIGNAL(showWelcomWindow()),&welcom_window,SLOT(replyShowWelcom())); //从订餐界面返回欢迎界面
    //关联购物车界面和订餐界面
    QObject::connect(&food_order_window,SIGNAL(showShopCartWindow()),&shop_cart_window,SLOT(replyShowShopCartWindow()));    //显示购物车界面
    QObject::connect(&food_order_window,SIGNAL(signal_add_intoshop_cart(Food)),
                     &shop_cart_window,SLOT(replyAddIntoShopCart(Food)));   //添加新食物进购物车
//    QObject::connect(&food_order_item,SIGNAL(addIntoShopCart(Food food)),&shop_cart_window,SLOT(replyAddIntoShopCart(Food food)));
    //关联购物车界面和订单列表界面
    QObject::connect(&shop_cart_window,SIGNAL(dataChange()),&admin_food_order_list_window,SLOT(replyDataChange()));
    QObject::connect(&shop_cart_window,SIGNAL(dataChange()),&user_food_order_list_window,SLOT(replyDataChange()));
    //关联管理员菜单界面和添加食物界面
    QObject::connect(&admin_main_window,SIGNAL(showAdminAddNewFoodWindow())
                     ,&admin_add_new_food_window,SLOT(replyShowAdminAddNewFoodWindow()));   //从管理员菜单界面跳转到添加食物界面
    //关联添加食物界面和订餐界面
    QObject::connect(&admin_add_new_food_window,SIGNAL(broadcast_data_changed()),&food_order_window,SLOT(data_changed()));  //刷新订餐界面
    //关联管理员菜单界面和添加管理员界面
    QObject::connect(&admin_main_window,SIGNAL(showAdminAddAdminWindow())
                     ,&admin_add_admin_window,SLOT(replyShowAdminAddAdminWindow()));    //从管理员菜单界面跳转到添加管理员界面
    //关联管理员菜单界面和订单列表界面
    QObject::connect(&admin_main_window,SIGNAL(showAdminFoodOrderListWindow())
                     ,&admin_food_order_list_window,SLOT(replyShowAdminFoodOrderListWindow())); //从管理员菜单界面跳转到订单列表界面
    //关联管理员菜单界面和管理食物界面
    QObject::connect(&admin_main_window,SIGNAL(showAdminFoodWindow())
                     ,&admin_food_window,SLOT(replyShowAdminFoodWindow())); //从管理员菜单界面跳转到管理食物界面
    //关联管理食物界面和订餐界面
    QObject::connect(&admin_food_window,SIGNAL(dataChange()),&food_order_window,SLOT(data_changed())); //刷新订餐界面

    return a.exec();
}


